<template xmlns:th="http://www.w3.org/1999/html">
  <div>
    <el-row gutter="10">
      <el-col span="24">
        <el-card shadow="never" class="card">
          <Search size='small' :searchVisible="searchVisible" :searchData.sync="searchData"  :searchForm = "searchForm" @refresh_list="index"></Search>
          <div>
            <el-button type="success"  size="mini" icon="el-icon-plus"  @click="add(ids)" v-if="checkPermission('/admin/produce/add.html')"  >添加</el-button>
            <!--            <el-button type="primary" :disabled="single" size="mini" icon="el-icon-edit"  >修改</el-button>-->
            <!--            <el-button type="danger" :disabled="multiple" size="mini" icon="el-icon-delete"   >删除</el-button>-->
            <!--            -->
          </div>

          <el-table  :data="list"  style="width: 100%">
            <el-table-column align="center" type="selection" width="42"></el-table-column>
            <el-table-column prop="bill_no" label="单据编号" width="180"></el-table-column>
            <el-table-column prop="task_bill_no" label="生产订单号" width="180"></el-table-column>
            <el-table-column prop="status" label="状态"  width="80">
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.status == '1'" size="mini" effect="dark">已审核</el-tag>
                <el-tag type="danger" v-if="scope.row.status == '0'" size="mini" effect="dark">未审核</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="stock_no" label="仓库" width="50"></el-table-column>
            <el-table-column prop="count" label="入库重量" width="80"></el-table-column>
            <el-table-column prop="audit_user_name" label="审核人" width="80"></el-table-column>
            <el-table-column prop="audit_time" label="审核时间" width="180"></el-table-column>
            <el-table-column prop="" label="文件上传" width="180">
              <template v-slot="scope">
                <div>
                  <el-upload
                      action="http://127.0.0.1:8083/api/sysPrdIn/upload"
                      :file-list="fileList"
                      :on-success="(row,res , file,fileList) => handleTableFileUpload(scope.row,res,file,fileList)">
                    <el-button size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" width="200" label="附件">
              <template slot-scope="scope" v-if="scope.row.url">
                <div>
                  <el-image style="width: 50px; height: 50px" :src="scope.row.url"></el-image>
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" width="200" label="下载">
              <template slot-scope="scope" v-if="scope.row.url">
                <div>
                  <el-button size="mini" type="primary"  @click="download(scope.row)" ><i class="el-icon-edit" />下载</el-button>
                </div>
              </template>
            </el-table-column>
            <el-table-column  :fixed="$store.getters.device !== 'mobile'?'right':false"  label="操作"  align="center" width="300">
              <template slot-scope="scope">
                <div v-if="scope.row.id">
                  <el-button size="mini" type="primary"  @click="update(scope.row)" v-if="checkPermission('/admin/produce/update.html')"><i class="el-icon-edit" />修改</el-button>
                  <el-button size="mini" type="danger"  @click="del(scope.row)" v-if="checkPermission('/admin/produce/delete.html')"><i class="el-icon-delete" />删除</el-button>
                  <el-button size="mini" type="success"  @click="detail(scope.row)" v-if="checkPermission('/admin/produce/detail.html')"><i class="el-icon-delete" />详情</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <Page :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit" @pagination="index" />
          <!--添加-->
          <Add  :show.sync="dialog.addDialogStatus" size="small" @refresh_list="index"></Add>
          <!--修改-->
          <Update :info="updateInfo" :show.sync="dialog.updateDialogStatus" size="small" @refresh_list="index"></Update>
          <!--详情-->
          <Detail :info="detailInfo" :show.sync="dialog.detailDialogStatus" size="small"  @refresh_list="index"></Detail>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Search from "@/components/common/Search.vue";
import Page from "@/components/common/Page.vue";
import TableTool from "@/components/common/TableTool.vue";
import Add from "@/views/admin/produce/add.vue";
import Update from "@/views/admin/produce/update.vue";
import Detail from "@/views/admin/produce/detail.vue"
import {confirm, param2Obj} from "@/utils/common";
import {index} from "@/api/admin/produce";
import {getInfo, del,update,detail} from "@/api/admin/produce";
export default {
  name: "produce",
  components: {
    Search,
    Page,
    TableTool,
    Add,
    Update,
    Detail
  },
  data() {
    return {
      dialog: {
        addDialogStatus: false,
        updateDialogStatus: false,
        jumpaUrlDialogStatus: false,
        detailDialogStatus:false
      },
      list: [],
      ids: [],
      page_data: {
        limit: 10,
        page: 1,
        total:20,
      },
      loading: true,
      searchData:{},
      searchForm:[],
      updateInfo:{},
      detailInfo:{},
    }
  },
  methods: {
    //初始化数据
    index(){
      let param = {limit:this.page_data.limit,page:this.page_data.page}
      Object.assign(param, this.searchData,param2Obj(this.$route.fullPath))
      this.loading = true
      index(param).then(res => {
        this.list = res.data.data
        this.page_data.total = res.data.total
        this.loading = false
      }).catch(err =>{

      })
      if (this.page_data.page == 1){
        this.searchForm = [
          {type:'input',label:'单据编号',prop:'bill_no',width:'150px'}
        ]
      }
    },
    //跳转到添加数据页面
    add(){
      this.dialog.addDialogStatus = true
    },
    //修改数据
    // updateExt(row,field){
    //   if(row.role_id){
    //     updateExt({id:row.id,[field]:row[field]}).then(res => {
    //       this.$message({message: '操作成功', type: 'success'})
    //     })
    //   }
    // },
    //修改单个数据
    update(row){
      console.log(row)
      let id = row.id ? row.id : this.ids.join(',')
      getInfo({id:id}).then(res => {
        if(res.status == 200){
          this.dialog.updateDialogStatus = true
          this.updateInfo = res.data
        }
      })
    },
    //删除数据
    del(row){
      console.log(row)
      confirm({content:'确定要操作吗'}).then(() => {
        let ids = row.id ? row.id : this.ids.join(',')
        del({id:ids}).then(res => {
          this.$message({message: res.msg, type: 'success'})
          this.index()
        })
      }).catch(() => {})
    },
    selection(selection) {
      this.ids = selection.map(item => item.role_id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    //查看详情
    detail(row){
      console.log(row)
      let id = row.id ? row.id : this.ids.join(',')
      detail({id:id}).then(res => {
        if (res.status == 200){
          this.dialog.detailDialogStatus = true
          this.detailInfo = res.data
        }
      })

    },
    //文件上传成功后的回调函数
    handleTableFileUpload(row,res, file, fileList){
      //上传成功后修改数据库
      row.url = res.response.msg
      update(row).then(res =>{
        if (res.status == 200){
          this.$message({message:'操作成功',type:'success'})
          this.$emit('')
        }
      })
    },
    //下载附件
    download(row){
      console.log(row.url)
      window.open(row.url)
      // download(row.url).then(res => {
      //   if (res.status == 200){
      //     this.$message({message:'正在下载',type:'success'})
      //   }
      // })
    }
  },
  mounted() {
    this.index()
  }
}
</script>

<style scoped>

</style>